package com.atguigu.interview.study.thread;

import com.atguigu.interview.enums.CountEnum;

import java.util.concurrent.CountDownLatch;

/**
 * @author gcq
 * @Create 2020-11-18
 * CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,调用线程会被阻塞
 * 其他线程调用countDown方法计数器减1(调用countDown方法时线程不会阻塞),
 * 当计数器的值变为0,因调用await方法被阻塞的线程会被唤醒,继续执行
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for(int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "\t 被灭");
                countDownLatch.countDown();
            }, CountEnum.forEach_CountEnum(i).getRetMessage()).start();
        }
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + "\t **********秦帝国 统一华夏");
    }


    private static void closeDoor() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for(int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "\t 上完自习，离开教室");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + "\t **********班长最后走人");
    }
}